<?php
/**
 *  clase que abstracta que engloba los servicios de la API de FedeX
 *
 *  @author HabaX
 */
class Class_Services_RateClient extends Class_Services
{

    public function __construct()
    {
        parent::__construct();

        $this->_path_to_wsld = "../application/Model/Class/Services/wsdl/RateService_v9.wsdl";

        ini_set("soap.wsdl_cache_enabled", "0");
        $this->_client = new SoapClient($this->_path_to_wsld , array('trace' => 1)); // Refer to http://us3.php.net/manual/en/ref.soap.php for more information
    }

    public function loadValues($rateParams)
    {
//        $this->_request['CurrencyType'] = array('Currency' => 'NMP');


        $this->_request['WebAuthenticationDetail'] = array('UserCredential' =>
            array('Key' => $this->_common->getProperty('key'), 'Password' => $this->_common->getProperty('password')));
        $this->_request['ClientDetail'] = array('AccountNumber' => $this->_common->getProperty('shipaccount'), 'MeterNumber' => $this->_common->getProperty('meter'));
        $this->_request['TransactionDetail'] = array('CustomerTransactionId' => ' *** Rate Request v9 using PHP ***');
        $this->_request['Version'] = array('ServiceId' => 'crs', 'Major' => '9', 'Intermediate' => '0', 'Minor' => '0');
        $this->_request['ReturnTransitAndCommit'] = true;
        $this->_request['RequestedShipment']['DropoffType'] = $rateParams['DropoffType']; // valid values REGULAR_PICKUP, REQUEST_COURIER, ...
        $this->_request['RequestedShipment']['ShipTimestamp'] = date('c');
        $this->_request['RequestedShipment']['ServiceType'] = $rateParams['ServiceType'];
        $this->_request['RequestedShipment']['PackagingType'] = $rateParams['PackagingType']; // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ...
        $this->_request['RequestedShipment']['TotalInsuredValue'] = array('Amount' => (int)$rateParams['TotalInsuredValue'] , 'Currency' => 'NMP');
        $this->_request['RequestedShipment']['Shipper'] = array('Address'=>$rateParams['AddressOrigen']);
        $this->_request['RequestedShipment']['Recipient'] = array('Address' => $rateParams['AddressDestiny']);

        $this->_request['RequestedShipment']['ShippingChargesPayment'] = array('PaymentType' => 'SENDER',
            'Payor' => array('AccountNumber' => $this->_common->getProperty('billaccount'),
                'CountryCode' => 'MX'));
        $this->_request['RequestedShipment']['RateRequestTypes'] = 'LIST';
        $this->_request['RequestedShipment']['PackageCount'] = $rateParams['PackageCount'];
        $this->_request['RequestedShipment']['PackageDetail'] = $rateParams['PackageDetail'];  //  Or PACKAGE_SUMMARY
        $this->_request['RequestedShipment']['RequestedPackageLineItems'] =  $rateParams['RequestedPackageLineItems'];
    }

    public function CallToFedex()
    {
        try
        {
            if($this->_common->setEndpoint('changeEndpoint'))
            {
                $newLocation = $this->_client->__setLocation(setEndpoint('endpoint'));
            }

            $response = $this->_client->getRates($this->_request);

            if($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR')
            {
                $rateReply = $response->RateReplyDetails;
                echo '<table border="1">';
                echo '<tr><td>Tipo de Servicio</td><td>Monto</td><td>IVA</td><td>Total</td><td>Fecha de Cotizacion</td></tr><tr>';
                $serviceType = '<td>' . $rateReply->ServiceType . '</td>';
                $amount = '<td>$' . number_format($rateReply->RatedShipmentDetails[1]->ShipmentRateDetail->TotalNetFedExCharge->Amount, 2, ".", ",") . '</td>';
                $taxes = '<td>$' . number_format($rateReply->RatedShipmentDetails[1]->ShipmentRateDetail->TotalTaxes->Amount, 2, ".", ",") . '</td>';
                $total = '<td>$' . number_format($rateReply->RatedShipmentDetails[1]->ShipmentRateDetail->TotalNetCharge->Amount, 2, ".", ",") . '</td>';
                if(array_key_exists('DeliveryTimestamp', $rateReply))
                {
                    $deliveryDate = '<td>' . $rateReply->DeliveryTimestamp . '</td>';
                }
                else if(array_key_exists('TransitTime', $rateReply))
                {
                    $deliveryDate = '<td>' . $rateReply->TransitTime . '</td>';
                }
                else
                {
                    $deliveryDate = '';
                }
                echo $serviceType . $amount . $taxes . $total . $deliveryDate;
                echo '</tr>';
                echo '</table>';

                $this->_common->printSuccess($this->_client, $response);
            }
            else
            {
                $this->_common->printError($this->_client, $response);
            }

            $this->_common->writeToLog($this->_client);    // Write to log file
        }
        catch(SoapFault $exception)
        {
            $this->_common->printFault($exception, $this->_client);
        }
    }

}